package com.sdw.message.dao;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import com.sdw.message.dao.base.DataBase;
import com.sdw.message.dao.base.DataReader;
import com.sdw.message.pojo.Student;

@Repository("studentDao")
public class StudentDao {

	@Resource
	private DataBase studentDatabase;
	
	public Iterable<Student> getAllStudent(){
		String sql = "select xh,ksh,sfzh,xm,xb,bj,xi from [dbo].[student$] order by xh desc";
		
		List<Student> list = new ArrayList<Student>();
		DataReader reader = null;
		try{
			reader = this.studentDatabase.executeQuery(sql);
			while(reader.next()){
				list.add(this.conver(reader));
			}
			
			return list;
		}finally{
			if(reader != null){
				reader.close();
			}
		}
	}
	
	private Student conver(DataReader reader){	
		int index = 1;
		Student student = new Student();
		
		student.setXh(reader.getString(index++));
		student.setKsh(reader.getString(index++));
		student.setSfzh(reader.getString(index++));
		student.setXm(reader.getString(index++));
		student.setXb(reader.getString(index++));
		student.setBj(reader.getString(index++));
		student.setXi(reader.getString(index++));
		
		return student;
	}
}
